package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.dongdongshop.config.AlipayConfig;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.po.AlipayBaseResult;
import com.dongdongshop.po.AlipayResultPO;
import com.dongdongshop.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("tradeController")
public class TradeController {
    @Reference
    private OrderService orderService;

    // 查询交易

    /**
     * @param WIDTQout_trade_no 订单号
     * @param WIDTQtrade_no     流水号
     */
    @RequestMapping("alipayTradeQuery")
    @ResponseBody
    public Result alipayTradeQuery(HttpServletRequest request, String WIDTQout_trade_no, String WIDTQtrade_no) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + WIDTQout_trade_no + "\"," + "\"trade_no\":\"" + WIDTQtrade_no + "\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        AlipayBaseResult alipayBaseResult = JSONObject.parseObject(result, AlipayBaseResult.class);
        AlipayResultPO response = alipayBaseResult.getAlipay_trade_query_response();
        if (response == null) {
            return Result.response(ResultEnum.ERROR);
        }
        //输出
        if (response.getCode() != null && response.getCode().intValue() == 10000) {
            return Result.response(ResultEnum.SUCCESS).setData(alipayBaseResult);
        }
        return Result.response(ResultEnum.ERROR);
    }

//退款

    /**
     * @param WIDTRout_trade_no   订单号
     * @param WIDTRtrade_no       流水号
     * @param WIDTRrefund_amount  退款金额
//     * @param WIDTRrefund_reason  退款原因
     * @param WIDTRout_request_no 退款请求号
     */

    @RequestMapping("alipayTradeRefund")
    public String alipayTradeRefund(Model model,
                                    String WIDTRout_trade_no,
                                    String WIDTRtrade_no,
                                    String WIDTRrefund_amount,
                                    String WIDTRout_request_no) throws AlipayApiException {
        String WIDTRrefund_reason ="不想要了";
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + WIDTRout_trade_no + "\","
                + "\"trade_no\":\"" + WIDTRtrade_no + "\","
                + "\"refund_amount\":\"" + WIDTRrefund_amount + "\","
                + "\"refund_reason\":\"" + WIDTRrefund_reason + "\","
                + "\"out_request_no\":\"" + WIDTRout_request_no + "\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //退款修改
        String Status = "6";
        orderService.updateOrderRefundStatus(WIDTRout_trade_no,Status);

        model.addAttribute("WIDTRout_trade_no",WIDTRout_trade_no);
        model.addAttribute("WIDTRtrade_no",WIDTRtrade_no);
        model.addAttribute("WIDTRrefund_amount",WIDTRrefund_amount);
        model.addAttribute("WIDTRrefund_reason",WIDTRrefund_reason);
        model.addAttribute("WIDTRout_request_no",WIDTRout_request_no);
        //输出
        return "refund";
    }



    /**
     *
     * @param WIDRQout_trade_no 订单号
     * @param WIDRQtrade_no     流水号
     * @param WIDRQout_request_no 退款请求号
     * @return
     */
    //退款查询
    @RequestMapping("alipayTradeFastpayRefundQuery")
    @ResponseBody
    public String alipayTradeFastpayRefundQuery(String WIDRQout_trade_no,
                                                String WIDRQtrade_no,
                                                String WIDRQout_request_no) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        //支付宝交易号
        //请二选一设置
        //请求退款接口时，传入的退款请求号，如果在退款请求时未传入，则该值为创建交易时的外部交易号，必填

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDRQout_trade_no +"\","
                +"\"trade_no\":\""+ WIDRQtrade_no +"\","
                +"\"out_request_no\":\""+ WIDRQout_request_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        AlipayTradeQueryRequest alipayTradeQueryRequest = JSONObject.parseObject(result, AlipayTradeQueryRequest.class);

        //输出
        return result;
    }

    @ResponseBody
    @RequestMapping("alipayTradeClose")
    //交易关闭
    public String alipayTradeClose(String WIDTCout_trade_no,String WIDTCtrade_no) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        //商户订单号，商户网站订单系统中唯一订单号
        //支付宝交易号
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTCout_trade_no +"\"," +"\"trade_no\":\""+ WIDTCtrade_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        return result;
    }


}
